#include "ClientOSCThread.h"
#include "ClientPacketListener.h"


ClientOSCThread::ClientOSCThread(OSCthulhuClient* client)
{
    this->client = client;
    this->receiveSocket = new UdpReceiveSocket(
            IpEndpointName( IpEndpointName::ANY_ADDRESS, client->getClientPort())
    );
    this->start();

}

void ClientOSCThread::run() {
    ClientPacketListener listener(client);
    mux.AttachSocketListener(this->receiveSocket,&listener);
    qDebug() << "press ctrl-c to end";
    mux.RunUntilSigInt();
    exec();
}

UdpReceiveSocket* ClientOSCThread::getReceiveSocket() {
    return this->receiveSocket;
}

void ClientOSCThread::quitConnection() {
    this->mux.AsynchronousBreak();
}

void ClientOSCThread::send(QString address, int port, osc::OutboundPacketStream p) {
    this->receiveSocket->SendTo(
            IpEndpointName(
                qPrintable(address),
                port
                ),
            p.Data(),p.Size()
        );
}

